// pages/staff/sncode.js
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  search(e) {
    var that = this;
    let params = e.detail.value;
    if (params.sncode.length < 6) {
      wx.showToast({
        title: '请填写SN码',
        icon: 'none',
        duration: 2000
      });
      return false;
    }

    app.util.request({
      url: 'entry/wxapp/snverify',
      data: {
        sncode: params.sncode,
      },
      cachetime: 30,
      success: function (res) {
        if (res.data.errno == 0) {
          that.setData({
            list: res.data.data.list
          });
        }
      },
      fail: function (res) {
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 2000
        });
        return false;
      }
    })
  },


  checkRecord: function (e) {
    var that = this;
    var recordid = e.currentTarget.dataset.id;
    var merchantid = e.currentTarget.dataset.merchantid
    console.log(recordid);
    var user = wx.getStorageSync('user');
    that.setData({
      isDisable: true
    });
    if (merchantid == user.merchantid) {
      app.util.request({
        url: 'entry/wxapp/checkRecord',
        data: {
          recordid: recordid,
          merchantid: user.merchantid,
          staffid: user.id
        },
        cachetime: 0,
        success: function (res) {
          if (res.data.errno == 0) {
            wx.showToast({
              title: '核销成功',
              icon: 'success',
              duration: 2000,
              success: function () {
                setTimeout(function () {
                  that.getlatest(recordid);
                }, 2000) //延迟时间
              }
            });

          }
        },
        fail: function (res) {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          });
          return false;
        }
      })
    } else {
      wx.showToast({
        title: '无权限核销该券',
        icon: 'none',
        duration: 2000
      });
    }

  },


  getlatest: function (recordid) {
    var that = this;
    app.util.request({
      url: 'entry/wxapp/getRecord',
      data: {
        recordid: recordid,
      },
      cachetime: 30,
      success: function (res) {
        if (res.data.errno == 0) {

          var list = [];
          list.push(res.data.data);
          that.setData({
            list: list
          });
        }
      },
      fail: function (res) {
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 2000
        });
        return false;
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})